package com.koron.common.core.business.common.service.impl;

import com.koron.bean.base.UserReadRecord;
import com.koron.common.core.business.common.mapper.UserReadRecordMapper;
import com.koron.common.core.business.common.service.UserReadRecordService;
import com.koron.common.core.business.readrecord.Read;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.util.CodeTools;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author twcao
 * @title: UserReadRecordServiceImpl
 * @projectName GDH_EAM_V2
 * @description: 用户已读记录
 * @date 2021/12/1614:42
 */
@Service
public class UserReadRecordServiceImpl implements UserReadRecordService {

    /**
     * 插入一条用户已读记录
     * @param record
     */
    @Override
    public void insert(UserReadRecord record) {
        Assert.notNull(record, "已读实体并能为空");
        Assert.hasText(record.getBizId(), "业务id不能为空");
        record.setAccount(ThreadLocalContext.get().getAccount());
        record.setId(CodeTools.getCode32());
        try (SessionFactory factory = new SessionFactory()) {
            factory.setDbEnv(ThreadLocalContext.get().getCurrDs());
            UserReadRecordMapper mapper = factory.getMapper(UserReadRecordMapper.class);
            if(mapper.getByBizId(record.getAccount(),record.getBizId()) > 0) {
                return;
            }
            mapper.insert(record);
        }
    }

    /**
     * 业务id, 账号用线程种获取
     *
     * @param bizId
     */
    @Override
    public void insert(String bizId) {
        insert(new UserReadRecord().setBizId(bizId));
    }

    /**
     * 根据业务id和账号，返回是否已读
     *
     * @param account 账号
     * @param bizId   业务id
     * @return 是否已读
     */
    @Override
    public boolean getByBizId(String account, String bizId) {
        Assert.notNull(account, "账号不能为空");
        Assert.notNull(bizId, "业务id不能为空");
        try (SessionFactory factory = new SessionFactory()) {
            factory.setDbEnv(ThreadLocalContext.get().getCurrDs());
            UserReadRecordMapper mapper = factory.getMapper(UserReadRecordMapper.class);
            return mapper.getByBizId(account, bizId) > 0;
        }
    }

    /**
     * 批量查询业务id和账号是否已读，业务id不超过1000个
     * @param account 账号
     * @param bizIds  业务id 非空
     * @return 返回已读的业务id
     */
    @Override
    public List<String> getByBizIds(String account, List<String> bizIds) {
        Assert.notNull(account, "账号不能为空");
        Assert.notEmpty(bizIds, "业务id集合不能为空");
        Assert.noNullElements(bizIds, "业务id不能为空");
        Assert.isTrue(CollectionUtils.size(bizIds) <= 1000, "业务id不得超过1000个");
        try (SessionFactory factory = new SessionFactory()) {
            factory.setDbEnv(ThreadLocalContext.get().getCurrDs());
            UserReadRecordMapper mapper = factory.getMapper(UserReadRecordMapper.class);
            return mapper.getByBizIds(account, bizIds);
        }
    }

    /**
     * 根据业务id和账号，返回是否已读
     * @param bizId   业务id
     * @return 是否已读
     */
    @Override
    public boolean getByBizId(String bizId) {
        return getByBizId(ThreadLocalContext.get().getAccount(), bizId);
    }

    /**
     * 批量查询业务id和账号是否已读
     * @param bizIds  业务id 非空
     * @return 返回已读的业务id
     */
    @Override
    public List<String> getByBizIds(List<String> bizIds) {
        return getByBizIds(ThreadLocalContext.get().getAccount(), bizIds);
    }

    /**
     * 标记已读字段
     * @param reads
     * @return
     */
    @Override
    public Collection<? extends Read> mark(Collection<? extends Read> reads) {
        if(CollectionUtils.isEmpty(reads)) {
            return reads;
        }
        List<String> hasRead = getByBizIds(reads.stream().map(Read::getId).collect(Collectors.toList()));
        reads.stream().forEach(read -> read.setRead(hasRead.contains(read.getId())));
        return reads;
    }
}
